<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/25
 * Time: 10:12
 */

namespace common\logics\taskcommod;

use common\widgets\Tool;
use Yii;
use yii\base\BaseObject;

class TaskCommodLogic extends BaseObject implements ITaskCommod
{
    /** @var \common\sources\write\taskcommod\TaskCommodAccess $WTaskCommodAccess **/
    private $WTaskCommodAccess;
    /** @var \common\sources\read\taskcommod\TaskCommodAccess  $RTaskCommodAccess **/
    private $RTaskCommodAccess;
    /*** @var \common\sources\read\buyuser\BuyUserAccess $RBuyUserAccess */
    private $RBuyUserAccess;
    /** @var \common\sources\read\shop\ShopAccess $RShopAccess ***/
    private $RShopAccess;
    /** @var \common\sources\read\taskorder\TaskOrderAccess $RTaskOrderAccess ***/
    private $RTaskOrderAccess;
    /** @var \common\sources\write\taskorder\TaskOrderAccess $WTaskOrderAccess **/
    private $WTaskOrderAccess;
    /*** @var \common\sources\read\valueservice\ValueServiceAccess  $RValueServiceAccess **/
    private $RValueServiceAccess;
    /*** @var \common\sources\read\taskcommodkeywords\TaskCommodKeywordAccess $RTaskCommodKeywordAccess **/
    private $RTaskCommodKeywordAccess;
    /*** @var \common\sources\write\taskcommodkeywords\TaskCommodKeywordAccess $WTaskCommodKeywordAccess **/
    private $WTaskCommodKeywordAccess;
    /** @var \common\sources\write\taskcommodlog\TaskCommodLogAccess $WTaskCommodLogAccess **/
    private $WTaskCommodLogAccess;
    /** @var \common\sources\read\taskshopimage\TaskShopImageAccess $RTaskShopImageAccess  ****/
    private $RTaskShopImageAccess;
    /** @var \common\sources\read\valueserviceprohibit\ValueServiceProhibitAccess  $RValueServiceProhibitAccess ***/
    private $RValueServiceProhibitAccess;
    /** @var \common\sources\read\praisekeywords\PraiseKeywordsAccess $RPraiseKeywordsAccess **/
    private $RPraiseKeywordsAccess;
    /** @var \common\sources\read\businessacount\BusinessAcountAccess $RBusinessAcountAccess ***/
    private $RBusinessAcountAccess;
    /** @var \common\sources\write\taskorderlog\BuyTaskOrderLogAccess  $WBuyTaskOrderLogAccess **/
    private $WBuyTaskOrderLogAccess;
    //买手佣金区间
    private $buy_commisse;
    //平台佣金区间
    private $plat_commisse;

    public function init()
    {
        $this->buy_commisse = require dirname(dirname(dirname(dirname(__FILE__)))) . '/config/buy_commisse-config.php';
        $this->plat_commisse = require dirname(dirname(dirname(dirname(__FILE__)))) . '/config/plat_commisse-config.php';
        $this->WTaskCommodAccess = Yii::$container->get("WTaskCommodAccess");
        $this->RTaskCommodAccess = Yii::$container->get("RTaskCommodAccess");
        $this->RTaskOrderAccess = Yii::$container->get("RTaskOrderAccess");
        $this->WTaskOrderAccess = Yii::$container->get("WTaskOrderAccess");
        $this->RBuyUserAccess = Yii::$container->get("RBuyUserAccess");
        $this->RShopAccess = Yii::$container->get("RShopAccess");
        $this->RValueServiceAccess = Yii::$container->get("RValueServiceAccess");
        $this->RTaskCommodKeywordAccess = Yii::$container->get("RTaskCommodKeywordAccess");
        $this->WTaskCommodKeywordAccess = Yii::$container->get("WTaskCommodKeywordAccess");
        $this->WTaskCommodLogAccess = Yii::$container->get("WTaskCommodLogAccess");
        $this->RTaskShopImageAccess = Yii::$container->get("RTaskShopImageAccess");
        $this->RValueServiceProhibitAccess = Yii::$container->get("RValueServiceProhibitAccess");
        $this->RPraiseKeywordsAccess = Yii::$container->get("RPraiseKeywordsAccess");
        $this->RBusinessAcountAccess = Yii::$container->get("RBusinessAcountAccess");
        $this->WBuyTaskOrderLogAccess = Yii::$container->get("WBuyTaskOrderLogAccess");
        parent::init(); // TODO: Change the autogenerated stub
    }
    public function taskType($data)
    {
        if(empty($data['plat_type']))
        {
            return Tool::returnJson([],401,"平台类型不能为空");
        }
        $plat_type = Yii::$app->params['plat_type'];
        $task_type = Yii::$app->params['plat_task_type'][$data['plat_type']];
        return Tool::returnJson([
            "plat_type" => $plat_type,
            "task_type" => $task_type,
        ],200,"成功");

    }
    public function getTaskType($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['plat_type']))
        {
            return Tool::returnJson([],402,"商家id不能为空");
        }
        //验证商家是否存在  根据商家id 去获取
        $buy = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buy)){
            return Tool::returnJson([],403,"商家会员不存在,请先注册");
        }
        $data['shop_type'] = $data['plat_type'];
        //获取店铺
        $shopes = $this->RShopAccess->getBusShop($data);
        if(count($shopes) == 0)
        {
            return Tool::returnJson([],3000,"商家暂未绑定店铺");
        }
        $task_types = Yii::$app->params['plat_task_type'][$data['plat_type']];
        foreach ($task_types as &$task_type)
        {
            $task_type['price'] = number_format($task_type['price'],2,".","");
        }
        $plates = Yii::$app->params['plat_type'];
        $res = [
            "plates" => $plates,
            "task_types" => $task_types,
            "shopes" => $shopes,
        ];
        return Tool::returnJson($res,200,"成功");
    }
    //发布任务第一步
    public function publishTask($data)
    {
        if(empty($data['bus_id'])){
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['plat_type'])){
            return Tool::returnJson([],402,"平台类型不能为空");
        }
        if(empty($data['shop_id'])){
            return Tool::returnJson([],403,"店铺id不能为空");
        }
        if(empty($data['shop_name'])){
            return Tool::returnJson([],405,"店铺名称不能为空");
        }
        if(empty($data['task_type'])){
            return Tool::returnJson([],406,"任务类型不能为空");
        }
        if(empty($data['commod_link'])){
            return Tool::returnJson([],407,"商品链接不能为空");
        }
        if(empty($data['title'])){
            return Tool::returnJson([],408,"商品标题不能为空");
        }
        if(empty($data['price'])){
            return Tool::returnJson([],409,"单件商品售价不能为空");
        }
        if(empty($data['num'])){
            return Tool::returnJson([],410,"每单拍 件数不能为空");
        }
        if(empty($data['task_num'])){
            return Tool::returnJson([],411,"发布任务数量不能为空");
        }
        if(empty($data['keywords']) && $data['task_type'] != 4){
            return Tool::returnJson([],412,"搜索关键子不能为空");
        }
        if(empty($data['main_url1']))
        {
            return Tool::returnJson([],413,"主图不能为空");
        }

        if($data['plat_type'] == 1 || $data['plat_type'] == 2)
        {
            if($data['task_type'] == 4 && empty($data['qr_code_url']))
            {
                return Tool::returnJson([],415,"二维码图片不能为空");
            }
            if($data['task_type'] == 2 && $data['task_num']>5)
            {
                return Tool::returnJson([],416,"淘宝天猫图文任务不能超过5单");
            }
            //判断链接对应的平台是否是对的
            if($data['plat_type'] == 1)
            {
                $arr  = explode("taobao",$data['commod_link']);
                if(count($arr)<2)
                {
                    return Tool::returnJson([],430,"非淘宝平台链接");
                }
            }
            if($data['plat_type'] == 2)
            {
                $arr  = explode("tmall",$data['commod_link']);
                if(count($arr)<2)
                {
                    return Tool::returnJson([],431,"非天猫平台链接");
                }
            }
        }
        if(count($data['keywords'])>5)
        {
            return Tool::returnJson([],417,"关键字不能超过5组");
        }
        if($data['task_num'] >100)
        {
            return Tool::returnJson([],418,"发布任务数量最多不能超过100单");
        }
        // 判断关键字数量跟 与任务数量进行比较
        $number = 0;
        if($data['task_type'] != 4)
        {
            foreach ($data['keywords'] as $tkey)
            {
                $number +=  $tkey['num'];
            }
            if($number != $data['task_num'])
            {
                return Tool::returnJson([],419,"发布任务数量与关键子单数不匹配");
            }
        }

        if(empty($data['port_type'])){
            return Tool::returnJson([],420,"端口类型不能为空");
        }
        //用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],421,'商家用户不存在');
        }
        if($buyUser['role'] !=2)
        {
            return Tool::returnJson([],422,'商家用户信息不对');
        }
        $shop = $this->RShopAccess->getBusShop(['bus_id'=> $data['bus_id'],'shop_type'=>$data['plat_type']]);
        if(count($shop) ==0)
        {
            return Tool::returnJson([],423,'店铺不存在');
        }
        $shopArr = array_column($shop,"id");
        if(!in_array($data['shop_id'],$shopArr))
        {
            return Tool::returnJson([],425,'非用户店铺');
        }
        $arr = array_keys($this->plat_commisse);
        $range = '';
        if(count($arr) > 0){
            foreach($arr as $item){
                if(Tool::getCommisse(number_format($data['price']* $data['num'],2,".",""),$item)){
                    $range = Tool::getCommisse(number_format($data['price']* $data['num'],2,".",""),$item);
                    continue;
                }
            }
        }
        if(empty($range)){
            return Tool::returnJson([],426,"商品价格区间不存在");
        }
        if(empty($this->plat_commisse[$range])){
            return Tool::returnJson([],427,"商品价格对应的佣金区间尚未配置");
        }
        //发布任务第一步 第二步 合并一起提交
        $plat_commission_arr = $this->plat_commisse[$range];
        $commission_type = 0;
        if(($data['plat_type'] == 1 || $data['plat_type'] == 2) && ($data['task_type'] == 1 || $data['task_type'] == 4) )
        {
            $commission_type = 1;
        }else if(($data['plat_type'] == 1 || $data['plat_type'] == 2) && $data['task_type'] == 2 )
        {
            $commission_type = 2;
        }
        $item_commission =  $plat_commission_arr[$commission_type];
        if(empty($item_commission))
        {
            return Tool::returnJson([],429,"商品对应的价格区间待配置中,请联系客服");
        }
        if(empty($this->buy_commisse[$range][$commission_type]))
        {
            return Tool::returnJson([],430,"买手佣金在配置中,请联系客服");
        }
        $buy_get_commisse = $this->buy_commisse[$range][$commission_type];

        $task_id = "TS".time().rand(100000,999999);
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $data['bus_phone'] = $buyUser['phone'];
            $data['task_id'] = $task_id;
            $data['com_amount'] = number_format($data['price']*$data['num']*$data['task_num'],2,'.','');        //商品交易金额
            $data['margin'] = number_format($data['price'] * $data['num'] * $data['task_num']*0.05,2,'.','');       //商品保证金
            $data['pay_task_amount'] = number_format(  $data['price'] * $data['num'] * $data['task_num']*1.05,2,'.','');      //支付任务金额
            $data['item_commisse'] = number_format($item_commission,2,".","");                                               //商家支付每单佣金
            $data['buy_get_commisse'] = number_format($buy_get_commisse,2,'.','');                                 //买手获得佣金
            $data['com_task_commisse'] = number_format($item_commission * $data['task_num'],2,'.','');               //商品支付任务佣金
            $data['pay_task_commisse'] = number_format($item_commission * $data['task_num'],2,'.','');               //支付任务佣金

            $keywordArr = $data['keywords'];
            $data['public_task_status'] = 3;
            $res = $this->WTaskCommodAccess->addTask($data);
            if($res){
                $id = Yii::$app->db->getLastInsertID();
                $arr = [
                    'task_commod_id' => $id,
                    'task_id' => $task_id,
                    'task_num' => $data['task_num'],
                ];
                //关键字
                $trr = [];
                $karr = [];
                if(count($keywordArr) >0)
                {
                    foreach ($keywordArr as &$keyword)
                    {
                        $karr['bus_id'] = $data['bus_id'];
                        $karr['task_commod_id'] = $id;
                        $karr['word'] = $keyword['word'];
                        $karr['num'] =  $keyword['num'];
                        $karr['create_time'] = time();
                        $trr[] = $karr;
                    }
                }
                //先删除
                $this->WTaskCommodKeywordAccess->delTaskCommodKeyword($id);
                $this->WTaskCommodKeywordAccess->addTaskCommodKeyword($trr);
                //增加日志记录
                $this->WTaskCommodLogAccess->addTaskCommodLog(['bus_id'=>$data['bus_id'],'task_status'=>1,"status_remark"=>'任务编辑中',"remark" => '',"operator"=>$buyUser['phone'],'task_id'=> $id]);
                $transaction->commit();
                return Tool::returnJson($arr,200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],428,"发布商品信息任务失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,'发布任务异常'.$e->getMessage());
        }
    }
    //商品发布任务
    public function modifyTaskType($data)
    {
        if(empty($data['bus_id'])){
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['plat_type'])){
            return Tool::returnJson([],402,"平台类型不能为空");
        }
        if(empty($data['shop_id'])){
            return Tool::returnJson([],403,"店铺id不能为空");
        }
        if(empty($data['shop_name'])){
            return Tool::returnJson([],405,"店铺名称不能为空");
        }
        if(empty($data['task_type'])){
            return Tool::returnJson([],406,"任务类型不能为空");
        }
        if(empty($data['task_commod_id']))
        {
            return Tool::returnJson([],407,"任务id不能为空");
        }

        if(empty($data['commod_link'])){
            return Tool::returnJson([],408,"商品链接不能为空");
        }
        if(empty($data['title'])){
            return Tool::returnJson([],409,"商品标题不能为空");
        }
        if(empty($data['price'])){
            return Tool::returnJson([],410,"单件商品售价");
        }
        if(empty($data['num'])){
            return Tool::returnJson([],411,"每单拍 件数不能为空");
        }
        if(empty($data['task_num'])){
            return Tool::returnJson([],412,"发布任务数量不能为空");
        }
        if(empty($data['keywords']) && $data['task_type'] != 4){
            return Tool::returnJson([],413,"搜索关键子不能为空");
        }
        if(empty($data['main_url1']))
        {
            return Tool::returnJson([],415,"主图不能为空");
        }
        if($data['plat_type'] == 1 || $data['plat_type'] == 2)
        {
            if($data['task_type'] == 4 && empty($data['qr_code_url']))
            {
                return Tool::returnJson([],416,"二维码图片不能为空");
            }
            if($data['task_type'] == 2 && $data['task_num']>5)
            {
                return Tool::returnJson([],417,"淘宝天猫图文任务不能超过5单");
            }
        }
        //查找主任务订单是否存在
        $task_commod = $this->RTaskCommodAccess->getTaskCommodById($data['task_commod_id']);
        if(empty($task_commod))
        {
            return Tool::returnJson([],418,"主任务订单不存在");
        }
        if(count($data['keywords'])>5)
        {
            return Tool::returnJson([],419,"关键字不能超过5组");
        }
        if($data['task_num'] >100)
        {
            return Tool::returnJson([],420,"发布任务数量最多不能超过100单");
        }
        // 判断关键字数量跟 与任务数量进行比较
        $number = 0;
        if($data['task_type'] != 4)
        {
            foreach ($data['keywords'] as $tkey)
            {
                $number +=  $tkey['num'];
            }
            if($number != $data['task_num'])
            {
                return Tool::returnJson([],419,"发布任务数量与关键子单数不匹配");
            }
        }

        if(empty($data['port_type'])){
            return Tool::returnJson([],422,"端口类型不能为空");
        }
        //用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],423,'商家用户不存在');
        }
        if($buyUser['role'] !=2)
        {
            return Tool::returnJson([],425,'商家用户信息不对');
        }
        $shop = $this->RShopAccess->getBusShop(['bus_id'=> $data['bus_id'],'shop_type'=>$data['plat_type']]);
        if(count($shop) ==0)
        {
            return Tool::returnJson([],426,'店铺不存在');
        }
        $shopArr = array_column($shop,"id");
        if(!in_array($data['shop_id'],$shopArr))
        {
            return Tool::returnJson([],427,'非用户店铺');
        }

        $arr = array_keys($this->plat_commisse);
        $range = '';
        if(count($arr) > 0){
            foreach($arr as $item){
                if(Tool::getCommisse(number_format($data['price']* $data['num'],2,".",""),$item)){
                    $range = Tool::getCommisse(number_format($data['price']* $data['num'],2,".",""),$item);
                    continue;
                }
            }
        }
        if(empty($range)){
            return Tool::returnJson([],426,"商品价格区间不存在");
        }
        if(empty($this->plat_commisse[$range])){
            return Tool::returnJson([],427,"商品价格对应的佣金区间尚未配置");
        }
        //发布任务第一步 第二步 合并一起提交
        $plat_commission_arr = $this->plat_commisse[$range];
        $commission_type = 0;
        if(($data['plat_type'] == 1 || $data['plat_type'] == 2) && ($data['task_type'] == 1 || $data['task_type'] == 4) )
        {
            $commission_type = 1;
        }else if(($data['plat_type'] == 1 || $data['plat_type'] == 2) && $data['task_type'] == 2 )
        {
            $commission_type = 2;
        }
        $item_commission =  $plat_commission_arr[$commission_type];
        if(empty($item_commission))
        {
            return Tool::returnJson([],429,"商品对应的价格区间待配置中,请联系客服");
        }
        if(empty($this->buy_commisse[$range][$commission_type]))
        {
            return Tool::returnJson([],430,"买手佣金在配置中,请联系客服");
        }
        $buy_get_commisse = $this->buy_commisse[$range][$commission_type];
        //发布任务第一步 第二步 合并一起提交
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $data['bus_phone'] = $buyUser['phone'];
            $data['com_amount'] = number_format($data['price']*$data['num']*$data['task_num'],2,'.','');        //商品交易金额
            $data['margin'] = number_format($data['price']*$data['num']*$data['task_num']*0.05,2,'.','');       //商品保证金
            $data['pay_task_amount'] = number_format( $data['price']*$data['num']*$data['task_num']*1.05,2,".","");      //支付任务金额
            $data['item_commisse'] = number_format($item_commission,2,".","");                                               //商家支付每单佣金
            $data['buy_get_commisse'] = number_format($buy_get_commisse,2,'.','');                                 //买手获得佣金
            $data['com_task_commisse'] = number_format($item_commission * $data['task_num'],2,'.','');               //商品支付任务佣金
            $data['pay_task_commisse'] = number_format($item_commission * $data['task_num'],2,'.','');               //支付任务佣金
            $keywordArr = $data['keywords'];
            $data['public_task_status'] = 3;
            $res = $this->WTaskCommodAccess->modifyTask($data);
            if($res){
                $arr = [
                    'task_commod_id' => $data['task_commod_id'],
                    'task_id' => $task_commod['task_id'],
                    'task_num' => $data['task_num'],
                ];
                //关键字
                $trr = [];
                $karr = [];
                if(count($keywordArr) >0)
                {
                    foreach ($keywordArr as &$keyword)
                    {
                        $karr['bus_id'] = $data['bus_id'];
                        $karr['task_commod_id'] = $data['task_commod_id'];
                        $karr['word'] = $keyword['word'];
                        $karr['num'] =  $keyword['num'];
                        $karr['create_time'] = time();
                        $trr[] = $karr;
                    }
                }
                //先删除
                $this->WTaskCommodKeywordAccess->delTaskCommodKeyword($data['task_commod_id']);
                $this->WTaskCommodKeywordAccess->addTaskCommodKeyword($trr);
                $transaction->commit();
                return Tool::returnJson($arr,200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],430,"发布商品信息任务失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,'修改选择任务异常'.$e->getMessage());
        }

    }
    //获取任务详情
    public function getShopTaskCommodDetail($data)
    {
        if(empty($data['bus_id'])){
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['task_commod_id']))
        {
            return Tool::returnJson([],402,"商品任务不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['bus_id'].$data['task_commod_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        //用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],406,'买手用户不存在');
        }
        $task_commod = $this->RTaskCommodAccess->getTaskCommodById($data['task_commod_id']);
        if(empty($task_commod))
        {
            return Tool::returnJson([],407,"商品任务不存在");
        }
        if($data['bus_id'] != $task_commod['bus_id'])
        {
            return Tool::returnJson([],408,"非本用户任务不能操作");
        }
        //获取主任务关键字
        $task_commod_keywords = $this->RTaskCommodKeywordAccess->getTaskCommodKeyword($data['task_commod_id']);
        $task_commod['keywords'] = $task_commod_keywords;
        return Tool::returnJson($task_commod,200,"成功");
    }

    //获取任务列表
    public function getTaskCommodList($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家用户id不能为空");
        }
        $page_num = $data['page_num'];
        $count = $this->RTaskCommodAccess->taskCommodCount($data);
        $total_arr = $data;
        $total_arr['task_commod_status'] = 0;
        $total_count = $this->RTaskCommodAccess->taskCommodCount($total_arr);
        $task_commod_having_count = $this->RTaskCommodAccess->taskCommodHavingCout($data);
        $task_commod_complete_count = $this->RTaskCommodAccess->taskCommodCompleteCout($data);
        $reArr = [];
        //我的主订单下面的子订单
        //已接手-待提交
        $receiveCount = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],1);
        //待打印快递单号
        $express_count = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],"2,3");
        //已打印  待发货
        $print_count = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],4);
        //已收货 待返款
        $ship_count = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],6);
        //驳回订单
        $reject_count = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],9);
        $reArr = [
                [
                    "order_status_name" => "已接手待提交",
                    "order_status_count" =>$receiveCount,
                    "order_status" =>1,
                ],
                [
                    "order_status_name" => "待打印快递单号",
                    "order_status_count" =>$express_count,
                    "order_status" =>3,
                ],
                [
                    "order_status_name" => "已打印待发货",
                    "order_status_count" =>$print_count,
                    "order_status" =>4,
                ],
                [
                    "order_status_name" => "已收货待返款",
                    "order_status_count" =>$ship_count,
                    "order_status" => 6,
                ],
                [
                    "order_status_name" => "驳回订单",
                    "order_status_count" =>$reject_count,
                    "order_status" => 9,
                ],
        ];
        $list = [];
        if($count>0)
        {
            $list = $this->RTaskCommodAccess->taskCommodList($data);
            //获取所有主订单下子订单 统计
            $taskArr = array_column($list,"id");
            $task_order_ids = implode(",",$taskArr);
            //获取进行中的子订单
            $havingArr = $this->RTaskOrderAccess->getHavingTaskOrderCount($task_order_ids,"1,2,3,4,5,6,7,9");
            //统计待发货
            $shippedArr = $this->RTaskOrderAccess->getTaskOrderCount($task_order_ids,4);
            //待返款
            $returnArr = $this->RTaskOrderAccess->getTaskOrderCount($task_order_ids,6);
            //未接单
//            $missOrderArr = 0;
            //已完成
            $completeArr = $this->RTaskOrderAccess->getHavingTaskOrderCount($task_order_ids,"8,12,13");

            foreach ($list as &$item)
            {
                $item['task_status_name'] = isset($item['task_status']) ? Yii::$app->params['task_commod_status'][$item['task_status']] : '';
                $item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s',$item['create_time']) : '';
                $item['having_name'] = "进行中";
                $item['having_count'] =0;
                $item['shipped_name'] = "待发货";
                $item['shipped_count'] =0;
                $item['return_name'] = "待返款";
                $item['return_count'] = 0;
                $item['missed_name'] = "未接单";
                $item['missed_count'] = 0;
                $item['complete_name'] = "已完成";
                $item['complete_count'] = 0;
                if(count($havingArr) >0)
                {
                    foreach ($havingArr as $haves)
                    {
                        if($item['id'] == $haves['task_commod_id'])
                        {
                            $item['having_count'] = $haves['count'];
                        }
                    }
                }
                //统计待发货
                if(count($shippedArr)>0)
                {
                    foreach ($shippedArr as $shipped)
                    {
                        if($item['id'] == $shipped['task_commod_id'])
                        {
                            $item['shipped_count'] = $shipped['count'];
                        }
                    }
                }
                //统计待返款
                if(count($returnArr) > 0)
                {
                    foreach ($returnArr as $returns)
                    {
                        if($item['id'] == $returns['task_commod_id'])
                        {
                            $item['return_count'] = $returns['count'];
                        }
                    }
                }
                //已完成
                if(count($completeArr))
                {
                    foreach ($completeArr as $complete)
                    {
                        if($item['id'] == $complete['task_commod_id'])
                        {
                            $item['complete_count'] = $complete['count'];
                        }
                    }
                }
                //未接单数量
                $item['missed_count'] = $item['task_num'] -$item['complete_count'] - $item['having_count'];
                $task_types =  Yii::$app->params['plat_task_type'][$item['plat_type']];
                foreach ($task_types as $task_ty)
                {
                    if($task_ty['task_type'] == $item['task_type'] )
                    {
                        $item['task_type_name'] = $task_ty['name'];
                    }

                }

            }
        }
        return Tool::returnJson([
            "list" => $list,
            "task_orders" => $reArr,
            "total_count" => $total_count,
            "count" => $count,
            "task_having_count"=> $task_commod_having_count,
            "task_complete_count" => $task_commod_complete_count,
            "page_num" => $page_num,
            "limit" => $data['limit']
        ],200,"成功");
    }
    //商家主任务详情
    public function getTaskCommodDetail($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家用户id不能为空");
        }
        if(empty($data['task_commod_id']))
        {
            return Tool::returnJson([],402,"主任务id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['bus_id'].$data['task_commod_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        //商家用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],406,'商家用户不存在');
        }
        $task_commod_detail = $this->RTaskCommodAccess->getTaskCommodById($data['task_commod_id']);
        if(empty($task_commod_detail))
        {
            return Tool::returnJson([],407,'主任务订单不存在');
        }
        $task_types = Yii::$app->params['plat_task_type'][$task_commod_detail['plat_type']];
        foreach ($task_types as &$task_ty)
        {
            if($task_ty['task_type'] == $task_commod_detail['task_type'])
            {
                $task_commod_detail['task_type_name'] = $task_ty['name'];
            }
        }
        $task_commod_detail['keywords'] = $this->RTaskCommodKeywordAccess->getTaskCommodKeyword($data['task_commod_id']);;
        $task_commod_detail['create_time'] = isset($task_commod_detail['create_time']) ? date('Y-m-d H:i:s',$task_commod_detail['create_time']): '';
        //待提交任务
        //已打印  待发货
        $shipped_count = $this->RTaskOrderAccess->getHavingCountTaskOrder($data['task_commod_id'],"2,3,4");
        //进行中
        $having_count = $this->RTaskOrderAccess->getHavingCountTaskOrder($data['task_commod_id'],"1,2,3,4,5,6,7,9");
        //已完成
        $complete_count = $this->RTaskOrderAccess->getHavingCountTaskOrder($data['task_commod_id'],"8,12,13");
        $no_task_over = $task_commod_detail['task_num'] - $having_count - $complete_count;

        $task_commod_detail['wating_deliver_count'] = $shipped_count;
        $task_commod_detail['having_count'] = $having_count;
        $task_commod_detail['complete_count'] = $complete_count;
        $task_commod_detail['no_task_over'] = $no_task_over;
        //任务进展部分
        $wating_submit_count = $this->RTaskOrderAccess->getTaskOrderStatusCount($data['task_commod_id'],1);
        $wating_submit = $this->RTaskOrderAccess->getTaskOrderByStatus($data['task_commod_id'],1);
        //费用合计
        $value_service = $this->RValueServiceAccess->getValueServiceByCommodId($data['task_commod_id']);
        $task_commod_detail['wating_submit_count'] = $wating_submit_count;

        //判断是否是图文评论
        $task_shop_image = [];
        if(($task_commod_detail['plat_type'] == 1 || $task_commod_detail['plat_type'] == 2) && $task_commod_detail['task_type'] == 2)
        {
            $task_shop_image = $this->RTaskShopImageAccess->getTaskShop($task_commod_detail['id']);
            if(count($task_shop_image) >0)
            {
                foreach ($task_shop_image as &$task_shop)
                {
                    $slide_imgs = [];
                    if(!empty($task_shop['slide_img1']))
                    {
                        array_push($slide_imgs,$task_shop['slide_img1']);
                    }
                    if(!empty($task_shop['slide_img2']))
                    {
                        array_push($slide_imgs,$task_shop['slide_img2']);
                    }
                    if(!empty($task_shop['slide_img3']))
                    {
                        array_push($slide_imgs,$task_shop['slide_img3']);
                    }
                    if(!empty($task_shop['slide_img4']))
                    {
                        array_push($slide_imgs,$task_shop['slide_img4']);
                    }
                    if(!empty($task_shop['slide_img5']))
                    {
                        array_push($slide_imgs,$task_shop['slide_img5']);
                    }
                    $task_shop['slide_imgs'] = $slide_imgs;
                }
            }
        }
        //账号余额
        $bus_account = $this->RBusinessAcountAccess->getAcount($data['bus_id']);

        $pay_arr = [];

        if(!empty($value_service))
        {
            $value_service['prohibit'] = $this->RValueServiceProhibitAccess->getValueServiceProhibit($value_service['id']);
            $value_service['custom_praise'] = [];
            $praise_key = [];
            if($value_service['praise'] == 1)
            {
                $value_service['praise_keys'] = [];
            }else if($value_service['praise'] == 3)
            {
                $value_service['custom_praise'] = $this->RPraiseKeywordsAccess->getPraiseKeywords($value_service['id']);
                $value_service['praise_keys'] = [];
            }else if($value_service['praise'] == 2)
            {
                $praise_key = explode("|",$value_service['praise_keys']);
                $value_service['praise_keys'] = is_array($praise_key) ? $praise_key :[$praise_key] ;
            }

            $value_service['pub_timing'] = isset($value_service['pub_timing']) ? date('Y-m-d H:i:s',$value_service['pub_timing']): '';
            $value_service['end_timing'] = isset($value_service['end_timing']) ? date('Y-m-d H:i:s',$value_service['end_timing']): '';

            if($value_service['return_type']> 0)
            {
                $pay_arr['return_type_pepper'] = !empty($value_service['return_type_pepper']) ? $value_service['return_type_pepper'] : 0.00;
            }
            if($value_service['interval_time'] > 0)
            {
                $pay_arr['interval_pepper'] = !empty($value_service['interval_pepper']) ? $value_service['interval_pepper'] : 0.00;
            }
            if($value_service['pub_timing'] >0)
            {
                $pay_arr['pub_timing_pepper'] = !empty($value_service['pub_timing_pepper']) ? $value_service['pub_timing_pepper'] :0.00;
            }
            if($value_service['chat_type']> 0)
            {
                $pay_arr['chat_pepper'] = !empty($value_service['chat_pepper']) ? $value_service['chat_pepper'] : 0.00 ;
            }
            if($value_service['exten_period'] >0)
            {
                $pay_arr['extend_pepper'] = !empty($value_service['extend_pepper']) ? $value_service['extend_pepper'] : 0.00 ;
            }
            if($value_service['credit_card'] >0)
            {
                $pay_arr['credit_card_pepper'] = !empty($value_service['credit_card_pepper']) ? $value_service['credit_card_pepper'] : 0.00;
            }
            if( $value_service['buy_sex'] >1)
            {
                $pay_arr['sex_pepper'] = !empty($value_service['sex_pepper']) ? $value_service['sex_pepper'] :0.00 ;
            }
            if($value_service['praise'] > 0)
            {
                $pay_arr['praise_pepper'] = !empty($value_service['praise_pepper']) ? $value_service['praise_pepper'] :0.00;
            }
            if($value_service['up_rank'] >0)
            {
                $pay_arr['up_rank_pepper'] = !empty($value_service['up_rank_pepper']) ? $value_service['up_rank_pepper'] :0.00;
            }
            if(!empty($value_service['prohibit']))
            {
                $pay_arr['prohibit_pepper'] = !empty($value_service['prohibit_pepper']) ? $value_service['prohibit_pepper'] : 0.00;
            }
            if(!empty($value_service['buy_age']))
            {
                $pay_arr['buy_age_pepper'] = !empty($value_service['buy_age_pepper']) ? $value_service['buy_age_pepper'] : 0.00 ;
            }

            if($value_service['reword_commise']>0)
            {
                $pay_arr['reword_commise'] = !empty($value_service['reword_commise']) ? $value_service['reword_commise'] : 0.00;
            }
            if($value_service['prio_audit']>0)
            {
                $pay_arr['prio_audit_pepper'] = !empty($value_service['prio_audit_pepper']) ? $value_service['prio_audit_pepper'] :0.00;
            }
            if($value_service['reword_commise']>0)
            {
                $pay_arr['reword_commise'] = !empty($value_service['reword_commise']) ? $value_service['reword_commise'] : 0.00;
            }
            if($value_service['expre_service'] >0)
            {
                $pay_arr['service_pepper'] = !empty($value_service['service_pepper']) ? $value_service['service_pepper'] : 0.00;
            }
            if($value_service['free_shipping'] == 2)
            {
                $pay_arr['freight_deposit'] = !empty($task_commod_detail['freight_deposit']) ? $task_commod_detail['freight_deposit'] :0.00;
            }
        }
        $pay_arr["task_num"] = $task_commod_detail['task_num'];
        $pay_arr['price']=  $task_commod_detail['price'];
        $pay_arr['num'] = $task_commod_detail['num'];
        $pay_arr['item_commisse'] = $task_commod_detail['item_commisse'];
        $pay_arr['margin'] = $task_commod_detail['margin'];
        $pay_arr['task_commod_id'] = $task_commod_detail['id'];
        $pay_arr['pay_task_amount'] = $task_commod_detail['pay_task_amount'];       //支付任务金额
        $pay_arr['pay_task_commisse'] = $task_commod_detail['pay_task_commisse'];   //支付任务佣金
        $pay_arr['service_amount'] = $task_commod_detail['service_amount'];         //增值服务总和
        $pay_arr['item_task_amount'] = $task_commod_detail['item_task_amount'];//每单任务金额（包含任务保证金额）
        $pay_arr['total_commisse'] = number_format($task_commod_detail['item_commisse']* $task_commod_detail['task_num'],2,".","");     //总佣金
        $pay_arr['total_amount'] = $task_commod_detail['total_amount'];                                           //花椒和商品金额总和
        $pay_arr['task_commod_id'] = $data['task_commod_id'];
        $pay_arr['value_service_id'] = isset($value_service['id']) ? $value_service['id'] :0;
        $pay_arr['ava_deposit'] = $bus_account['ava_deposit'];
        $pay_arr['ava_pepper'] = $bus_account['ava_pepper'];
        //店铺信息id
        $shop_detail = $this->RShopAccess->getShopDetail($task_commod_detail['shop_id']);
        //是否打印
        $is_print = $shop_detail['is_print'] == 1 ? 1:0;

        return Tool::returnJson([
            "task_commod" => $task_commod_detail,
            "wating_submit" => $wating_submit,
            "value_service" => $value_service,
            "task_shop_image" => $task_shop_image,
            "task_pay_account" => $bus_account,
            "pay_value_service" => $pay_arr,
            "is_print" => $is_print,
        ],200,"成功");
    }
    //获取主订单详情 列表分页搜索
    public function getDetailTaskOrder($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家用户id不能为空");
        }
        if(empty($data['task_commod_id']))
        {
            return Tool::returnJson([],402,"主任务id不能为空");
        }
        if(empty($data['task_order_status']))
        {
            return Tool::returnJson([],403,"任务状态不能为空");
        }
        $count = $this->RTaskOrderAccess->getTaskOrderDetailCount($data);
        $list = [];
        $value_service = [];
        if($count>0)
        {
            $list = $this->RTaskOrderAccess->getTaskOrderDetailList($data);
            foreach ($list as &$item)
            {
                $item['express_num'] = isset($item['express_num']) ? $item['express_num'] : '';
                $item['colour'] = isset($item['colour']) ? $item['colour'] : '';
                $item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s',$item['create_time']) : '';
                $item['order_status_name'] = Yii::$app->params['task_order_status_name'][$item['order_status']];
                $item['keywords'] =  isset($item['keywords'])? $item['keywords']: '';
            }
            $value_service = $this->RValueServiceAccess->getValueServiceByCommodId($data['task_commod_id']);
        }
        return Tool::returnJson([
            "count" => $count,
            "list" =>$list,
            "value_service" => $value_service,
            "page_num" => $data['page_num'],
        ],200,"成功");

    }

    //商家子订单任务列表
    public function getTaskOrderList($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家用户id不能为空");
        }
        $arr = [
            "bus_id" => $data['bus_id'],
            "shop_type" => $data['plat_type'],
        ];
        //获取商家绑定店铺 列表
        $shop = $this->RShopAccess->getBusShop($arr);
        $page_num = $data['page_num'];
        $reArr = [];
        //我的主订单下面的子订单
        //已接手-待提交
        $receiveCount = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],1);
        //待打印快递单号
        $express_count = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],"2,3");
        //已打印  待发货
        $print_count = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],4);
        //已收货 待返款
        $ship_count = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],6);
        //驳回订单
        $reject_count = $this->RTaskOrderAccess->getBusinessTaskOrderCount($data['bus_id'],9);
        $reArr = [
            [
                "order_status_name" => "已接手待提交",
                "order_status_count" =>$receiveCount,
                "order_status" =>1,
            ],
            [
                "order_status_name" => "待打印快递单号",
                "order_status_count" =>$express_count,
                "order_status" =>3,
            ],
            [
                "order_status_name" => "已打印  待发货",
                "order_status_count" =>$print_count,
                "order_status" =>4,
            ],
            [
                "order_status_name" => "已收货 待返款",
                "order_status_count" =>$ship_count,
                "order_status" => 6,
            ],
            [
                "order_status_name" => "驳回订单",
                "order_status_count" =>$reject_count,
                "order_status" => 9,
            ],
        ];
        //统计商家子任务数量
        $count = $this->RTaskOrderAccess->getBusTaskOrderCount($data);
        $list = [];

        if($count >0)
        {
            $list = $this->RTaskOrderAccess->getBusTaskOrderList($data);
            foreach($list as &$item)
            {
                $task_typeArr =  Yii::$app->params['plat_task_type'][$item['plat_type']];
                foreach ($task_typeArr as $task_ty)
                {
                    if($task_ty['task_type'] == $item['task_type'])
                    {
                        $item['task_type_name'] = $task_ty['name'];
                    }
                }
                $item['order_status_name'] = isset($item['order_status']) ? Yii::$app->params['task_order_status_name'][$item['order_status']] : '未知';
                $item['create_time'] = isset($item['create_time']) ? date('Y-m-d H:i:s',$item['create_time']) : "";
            }
        }
        $plats = Yii::$app->params['plat_type'];
        return Tool::returnJson([
            "list" => $list,
            "count" => $count,
            "task_order_status" => $reArr,
            "plats" => $plats,
            "shopes" => $shop,
            "page_num" => $page_num,
            "limit" => $data['limit'],
        ],200,"成功");
    }
    //发货操作
    public function deliverGoods($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家用户id不能为空");
        }
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],402,"商家子订单id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if($data['safetyCode'] != md5($data['bus_id'].$data['task_order_id']))
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        //商家用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],406,'商家用户不存在');
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderDetial($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],407,'子订单不存在');
        }
        if($task_order['order_status'] != 4)
        {
            return Tool::returnJson([],408,'该状态不能发货');
        }
        $transaction = Yii::$app->db->beginTransaction();
        try{
            //发货操作
            $res = $this->WTaskOrderAccess->deliverGoods(['id'=>$data['task_order_id'],"order_status"=>5]);
            if($res)
            {
                //添加接任务日志
                $task_order_arr = [
                    "buy_id" => $data['bus_id'],
                    "task_status" => 5,
                    "status_remark" => "商家已发货待收货",
                    "remark" => '已发货',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyUser['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],425,"添加商家操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],409,"发货失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"发货异常".$e->getMessage());
        }
    }
    //批量发货操作
    public function bulkShip($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(count($data['task_order_info']) ==0)
        {
            return Tool::returnJson([],402,"批量操作数据不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['bus_id'].json_encode($data['task_order_info'])) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        //商家用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],406,'商家用户不存在');
        }
        $task_orderArr = array_column($data['task_order_info'],"task_order_id");
        $task_order_ids = implode(",",$task_orderArr);
        //查询待发货子订单
        $task_orders = $this->RTaskOrderAccess->getTaskOrdersByIds($task_order_ids,4);
        if(count($task_orderArr) != count($task_orders))
        {
            return Tool::returnJson([],407,"存在非待发货子订单");
        }
        $transaction = Yii::$app->db->beginTransaction();
        try{
            //开始批量操作
            $res = $this->WTaskOrderAccess->batchTaskOrder($task_orderArr);
            if($res)
            {
                foreach ($data['task_order_info'] as $task_order)
                {
                    //添加接返款日志
                    $task_order_arr = [
                        "buy_id" => $data['bus_id'],
                        "task_status" => 5,
                        "status_remark" => "商家已发货，待买手确认收货",
                        "remark" => '已发货待收货',
                        "task_order_id" => $task_order['task_order_id'],
                        "operator" => $buyUser['phone'],
                    ];
                    $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                    if(!$task_order_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],425,"添加商家操作日志失败");
                    }
                }

                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],408,"操作失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"批量操作异常");
        }
    }
    //获取子订单详情
    public function getTaskOrderDetail($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],402,"子订单id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['bus_id'].$data['task_order_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        //商家用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],406,'商家用户不存在');
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderDetial($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],407,'该任务不存在');
        }
        if($task_order['bus_id'] != $data['bus_id'])
        {
            return Tool::returnJson([],407,'非该商家用户子订单');
        }

        $task_order['express_num'] = isset($task_order['express_num']) ? $task_order['express_num'] : "";
        $task_order['keyword_url'] = isset($task_order['keyword_url']) ? $task_order['keyword_url'] : "";
        $task_order['parise_url'] = isset($task_order['parise_url']) ? $task_order['parise_url'] : "";
        $task_order['tao_parise_url'] = isset($task_order['tao_parise_url']) ? $task_order['tao_parise_url'] : "";

        $task_order['order_status_name'] = Yii::$app->params['task_order_status_name'][$task_order['order_status']];
        $task_order['search_time'] = isset($task_order['search_time']) ? date('Y-m-d H:i:s',$task_order['search_time']) : '--';
        $task_order['browse_time'] = isset($task_order['browse_time']) ? date('Y-m-d H:i:s',$task_order['browse_time']) : "--";
        $task_order['order_time'] = isset($task_order['order_time']) ? date('Y-m-d H:i:s',$task_order['order_time']) : "--";
        $task_order['cancel_time'] = isset($task_order['cancel_time']) ? date('Y-m-d H:i:s',$task_order['cancel_time']) : "--";
        $task_order['merchant_time'] = isset($task_order['merchant_time']) ? date('Y-m-d H:i:s',$task_order['merchant_time']) : "--";
        $task_order['favorable_time'] = isset($task_order['favorable_time']) ? date('Y-m-d H:i:s',$task_order['favorable_time']) : "--";
        $task_order['plat_return_time'] = isset($task_order['plat_return_time']) ? date('Y-m-d H:i:s',$task_order['plat_return_time']) : "--";
        $task_order['confirm_time'] = isset($task_order['confirm_time']) ? date('Y-m-d H:i:s',$task_order['confirm_time']) : "--";
        $task_order['give_up_time'] = isset($task_order['give_up_time']) ? date('Y-m-d H:i:s',$task_order['give_up_time']) : "--";
        $task_order['reject_time'] = isset($task_order['reject_time']) ? date('Y-m-d H:i:s',$task_order['reject_time']) : "--";
        $task_order['create_time'] = isset($task_order['create_time']) ? date('Y-m-d H:i:s',$task_order['create_time']) : "--";
        $task_order['return_type_name'] = isset($task_order['return_type']) ? ($task_order['return_type'] == 1 ? "平台返款" :"商家返款"):"";
        $task_order['port_type_name'] = isset($task_order['port_type']) ? ($task_order['port_type'] == 1 ? "电脑端" :"手机端"):"";
        $task_types = Yii::$app->params['plat_task_type'][$task_order['plat_type']];
        foreach ($task_types as &$task_ty)
        {
            if($task_ty['task_type'] == $task_order['task_type'])
            {
                $task_order['task_type_name'] = $task_ty['name'];
            }

        }
        return Tool::returnJson($task_order,200,"成功");
    }
    //商家返款
    public function merchantReturn($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],402,"子订单id不能为空");
        }
//        if(empty($data['refund_amount']))
//        {
//            return Tool::returnJson([],403,"商家返款金额不能为空");
//        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],405,"安全码不能为空");
        }
        if(md5($data['bus_id'].$data['task_order_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],406,"安全码不正确");
        }
        //商家用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],407,'商家用户不存在');
        }
        $task_order = $this->RTaskOrderAccess->getTaskOrderDetial($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],408,'该任务不存在');
        }
        if($task_order['bus_id'] != $data['bus_id'])
        {
            return Tool::returnJson([],409,'不是该商家下的子任务');
        }
        if(!($task_order['order_status'] == 6 || $task_order['order_status'] == 9) )
        {
            return Tool::returnJson([],410,'该状态不可返款');
        }
        if($task_order['order_status'] == 9 && empty($data['refund_amount']))
        {
            return Tool::returnJson([],403,"买手驳回商家返款金额不能为空");
        }
        //返款操作
        $transaction = Yii::$app->db->beginTransaction();
        $res = false;
        try{
            if(!empty($data['refund_amount']))
            {
                $res = $this->WTaskOrderAccess->rejectRefund(['id'=> $data['task_order_id'],"refund_amount"=> $data['refund_amount'],"order_status"=> 7,"bus_pay_url" => $data['bus_pay_url'],"serial_num"=> $data['serial_num']]);
            }else
            {
                $res = $this->WTaskOrderAccess->rejectBusRefund(['id'=> $data['task_order_id'],"order_status"=> 7,"bus_pay_url" => $data['bus_pay_url'],"serial_num"=> $data['serial_num'],"refund_amount" => $task_order['actual_refunds']]);
            }
            if($res)
            {
                //添加接返款日志
                $task_order_arr = [
                    "buy_id" => $data['bus_id'],
                    "task_status" => 7,
                    "status_remark" => "商家已返款，待买手确认",
                    "remark" => '已返款待确认',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyUser['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],425,"添加商家操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,'商家返款成功');
            }
            $transaction->rollBack();
            return Tool::returnJson([],411,"返款失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"返款异常".$e->getMessage());
        }
    }
    //商家批量操作返款
    public function batchRefund($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(count($data['task_order_info']) ==0)
        {
            return Tool::returnJson([],402,"批量操作数据不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['bus_id'].json_encode($data['task_order_info'])) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        //商家用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],406,'商家用户不存在');
        }
        $task_orderArr = array_column($data['task_order_info'],"task_order_id");
        $task_order_ids = implode(",",$task_orderArr);
        //查询待返款子订单
        $task_orders = $this->RTaskOrderAccess->getTaskOrdersByIds($task_order_ids,6);
        if(count($task_orderArr) != count($task_orders))
        {
            return Tool::returnJson([],407,"存在非待返款子订单");
        }
        //返款操作
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->batchRefund($task_orderArr);
            if($res)
            {
                foreach ($data['task_order_info'] as $task_order)
                {
                    //添加接返款日志
                    $task_order_arr = [
                        "buy_id" => $data['bus_id'],
                        "task_status" => 7,
                        "status_remark" => "商家返款，待买手确认返款",
                        "remark" => '已返款待确认',
                        "task_order_id" => $task_order['task_order_id'],
                        "operator" => $buyUser['phone'],
                    ];
                    $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                    if(!$task_order_res)
                    {
                        $transaction->rollBack();
                        return Tool::returnJson([],425,"添加商家操作日志失败");
                    }
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],408,"批量操作失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"操作异常");
        }
    }
    //商家驳回
    public function merchantDissmiss($data)
    {
        if(empty($data['bus_id']))
        {
            return Tool::returnJson([],401,"商家id不能为空");
        }
        if(empty($data['task_order_id']))
        {
            return Tool::returnJson([],402,"子订单id不能为空");
        }
        if(empty($data['safetyCode']))
        {
            return Tool::returnJson([],403,"安全码不能为空");
        }
        if(md5($data['bus_id'].$data['task_order_id']) != $data['safetyCode'])
        {
            return Tool::returnJson([],405,"安全码不正确");
        }
        if(empty($data['refund_amount']))
        {
            return Tool::returnJson([],406,"商家驳回金额不能为空");
        }
        //商家用户
        $buyUser = $this->RBuyUserAccess->getBuyUserInfo($data['bus_id']);
        if(empty($buyUser)){
            return Tool::returnJson([],407,'商家用户不存在');
        }
        //商品任务子订单
        $task_order = $this->RTaskOrderAccess->getTaskOrderDetial($data['task_order_id']);
        if(empty($task_order))
        {
            return Tool::returnJson([],408,'该任务不存在');
        }
        if($task_order['bus_id'] != $data['bus_id'])
        {
            return Tool::returnJson([],409,'不是该商家下的子任务');
        }
        //判断子任务状态是否可以驳回
        if(!($task_order['order_status'] == 9 ||$task_order['order_status'] == 6) )
        {
            return Tool::returnJson([],410,'该状态不可驳回');
        }
        //进行驳回操作
        $transaction = Yii::$app->db->beginTransaction();
        try{
            $res = $this->WTaskOrderAccess->busReject(["id"=>$data['task_order_id'],"order_status"=>7,"refund_amount" => $data['refund_amount']]);
            if($res)
            {
                //添加接返款日志
                $task_order_arr = [
                    "buy_id" => $data['bus_id'],
                    "task_status" => 7,
                    "status_remark" => "商家驳回，待买手确认返款",
                    "remark" => '已返款待确认',
                    "task_order_id" => $data['task_order_id'],
                    "operator" => $buyUser['phone'],
                ];
                $task_order_res = $this->WBuyTaskOrderLogAccess->addTaskOrderLog($task_order_arr);
                if(!$task_order_res)
                {
                    $transaction->rollBack();
                    return Tool::returnJson([],425,"添加商家操作日志失败");
                }
                $transaction->commit();
                return Tool::returnJson([],200,"成功");
            }
            $transaction->rollBack();
            return Tool::returnJson([],411,"商家驳回失败");
        }catch (\Exception $e)
        {
            $transaction->rollBack();
            return Tool::returnJson([],5000,"商家驳回异常".$e->getMessage());
        }
    }

    //获取发货地
    public function getCommodAddr(){
        $arr = [
            [
               ["label" => "北京"],
               ["label" => "上海"],
               ["label" => "广州"],
               ["label" => "深圳"],
               ["label" => "杭州"],
               ["label" => "海外"],
               ["label" => "江浙沪"],
               ["label" => "京津冀"],
               ["label" => "港澳台"],
            ],
            [
                ["label" => "长沙"],
                ["label" => "长春"],
                ["label" => "成都"],
                ["label" => "重庆"],
                ["label" => "大连"],
                ["label" => "东莞"],
                ["label" => "佛山"],
                ["label" => "福州"],
                ["label" => "贵阳"],
                ["label" => "合肥"],
                ["label" => "金华"],
                ["label" => "济南"],
                ["label" => "嘉兴"],
                ["label" => "昆明"],
                ["label" => "宁波"],
                ["label" => "南昌"],
                ["label" => "南京"],
                ["label" => "青岛"],
                ["label" => "泉州"],
                ["label" => "沈阳"],
                ["label" => "苏州"],
                ["label" => "天津"],
                ["label" => "温州"],
                ["label" => "无锡"],
                ["label" => "武汉"],
                ["label" => "西安"],
                ["label" => "厦门"],
                ["label" => "郑州"],
                ["label" => "中山"],
                ["label" => "石家庄"],
                ["label" => "哈尔滨"],
            ],
            [
                ["label" => "安徽"],
                ["label" => "福建"],
                ["label" => "甘肃"],
                ["label" => "广东"],
                ["label" => "广西"],
                ["label" => "贵州"],
                ["label" => "海南"],
                ["label" => "河北"],
                ["label" => "河南"],
                ["label" => "湖北"],
                ["label" => "湖南"],
                ["label" => "江苏"],
                ["label" => "江西"],
                ["label" => "吉林"],
                ["label" => "辽宁"],
                ["label" => "宁夏"],
                ["label" => "青海"],
                ["label" => "山东"],
                ["label" => "山西"],
                ["label" => "陕西"],
                ["label" => "云南"],
                ["label" => "四川"],
                ["label" => "西藏"],
                ["label" => "新疆"],
                ["label" => "浙江"],
                ["label" => "澳门"],
                ["label" => "香港"],
                ["label" => "台湾"],
                ["label" => "内蒙古"],
                ["label" => "黑龙江"],
            ]
        ];
        return Tool::returnJson($arr,200,"成功");
    }
}